<?php namespace Phpcmf\Controllers;
/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */
class Liebiao extends \Phpcmf\Home\Module
{

	public function index() {
		// 获取get参数
		if($this->uid){
			
		
			$catid = (int)\Phpcmf\Service::L('input')->get('catid');
			$pagesize = (int)\Phpcmf\Service::L('input')->get('pagesize'); // 页数量
			$page = (int)\Phpcmf\Service::L('input')->get('page'); // 当前页
			$shuxing = (int)\Phpcmf\Service::L('input')->get('shuxing'); // 属性 0是最新，1是最热，2热评
			if($shuxing == 1){
				$order = 'hits';
			} elseif($shuxing == 2){
				$order = 'comments';
			} else {
				$order = 'id';
			}
			if(!$pagesize){
				$pagesize = 20; // 默认值
			}
			if(!$page){
				$page = 1; // 默认第一页
			}
			if($catid){
				$page_data = $page; // 从第一页开始
				$per_page = $pagesize; // 每页20条
				$offset = ($page_data - 1) * $per_page;
				$daohang_0 = \Phpcmf\Service::M()->db->table('1_duixin_category')->where('id', $catid)->get()->getRowArray();
				// 初始化为空数组
				$childids = [];
				// 处理childids
				if ($daohang_0['childids']) {
					$childids = explode(',', $daohang_0['childids']);
				}

				$in_id = [];
				foreach ($childids as $i) {
					$in_id[] = $i;
				}
				$shuju_data = \Phpcmf\Service::M()->table('1_duixin')->where('shouxinren', $this->uid)->where_in('catid', $in_id)->order_by($order, 'desc')->limit($per_page, $offset)->getAll();
			} else {
				$page_data = $page; // 从第一页开始
				$per_page = $pagesize; // 每页20条
				$offset = ($page_data - 1) * $per_page;
				$shuju_data = \Phpcmf\Service::M()->table('1_duixin')->where('shouxinren', $this->uid)->order_by($order, 'desc')->limit($per_page, $offset)->getAll();
			}
			
			// 循环 $shuju_data ， 把 $shuju_data['content'] 中的图片路径替换为 域名 + 图片路径
			$list_data = [];
			foreach ($shuju_data as $key => $val) {
				$quan_data = \Phpcmf\Service::M()->db->table('1_duixin_data_0')->where('id', $val['id'])->get()->getRowArray();
				$daohang_data = \Phpcmf\Service::M()->db->table('1_duixin_category')->where('id', $val['catid'])->get()->getRowArray();
				// 过滤pid中的0值
				$pid = $daohang_data['pid']; // 假设原始pid是标量
				if (is_array($pid)) {
					$pid = array_filter($pid, function($value) {
						return $value !== 0;
					});
					// 如果过滤后数组为空，设置默认值1
					if (empty($pid)) {
						$pid = [1];
					}
				}
				$daohang_data_1 = \Phpcmf\Service::M()->db->table('1_duixin_category')->where('id', $pid)->get()->getRowArray();

				// 内容图片列表
				$tupian_data = dr_get_content_img($val['neirong']);
				$tupian_list = [];
				foreach ($tupian_data as $k => $v) {
					// 去除&quot;及前面的内容
					$v = preg_replace('/.*&quot;/', '', $v);
					$tupian_list[] = dr_thumb($v, 250, 250, 0, 'crop');
					
				}
				// 头像
				if(dr_avatar($val['uid'])){
					$touxiang = dr_avatar($val['uid']);
				} else {
					$touxiang = 1;
				}
				// 问答图 
				$wendatu_list = [];
				foreach ($val['wendatu'] as $k => $v) {
					if ($k <= 8) {
						$wendatu_list[] = dr_thumb($v['file'], 250, 250, 0, 'crop');
					}
				}
				// 名称
				$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $val['uid'])->get()->getRowArray();
				$user = dr_member_info($val['uid']);
				if($userweixin){
					$mingcheng = $userweixin['nickname'];
				} else if($user['name']) {
					$mingcheng = $user['name'];
				} else {
					$mingcheng = $user['username'];
				}
				
				
				// 帖子内容列表
				$list_data[] = [
					'id' => $val['id'],
					'cid' => $val['catid'],
					'uid' => $val['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $val['title'],
					'tupian' => $tupian_list,
					'wendatu' => $wendatu_list,
					'jianjie' => dr_strcut(dr_clearhtml($val['content']), 100),
					'fabushijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
					'name' => $daohang_data_1['name'].'/'.$daohang_data['name'],
					'catid' => $val['catid'],
					'pid' => $pid,
					'pinglun' => $val['comments'],
					'moshi' => $val['moshi'],
					'shangjin' => $val['shangjin'],
					'zhuangtai' => $val['zhuangtai'],
					'yidu' => $val['yidu'],
				];
			}
			$tongji = \Phpcmf\Service::M()->db->table('1_duixin')->countAllResults();
			$daan_tongji = \Phpcmf\Service::M()->db->table('1_duixin')->countAllResults();

			$data = [
				'list' => $list_data,
				'tongji' => $tongji,
				'in_id' => $in_id,
				'daan_tongji' => $daan_tongji,
				
			];

			$this->_json('1', '读取成功', $data);
		} else {
			$this->_json('0', '读取失败');
		}
	}

}
